package Sudoku3.rules;
import Sudoku3.structure.bucket;
import Sudoku3.structure.cell;
import Sudoku3.structure.group;


public class rule05_naked_set {
	
	//within a group, (n) cells have the same exact (n) possibilities
	public rule05_naked_set(group g) {
				
		if (g instanceof bucket) { return; } //return if g is a bucket (this test is not valid).
		if (g.getSolvedCellCount() == 9) { return; } // return if the whole group is solved.
		
		//outer loop iterate cells
		for (cell c : g) {
			if (c.isSolved()) { continue; }
			bucket b = new bucket();
			b.add(c);
			
			//inner loop iterate other cells in group
			for (cell x : g) {
				if ( (x.isSolved()) || (x == c) ) { continue; }
				if (x.getPossible().equals(c.getPossible())) { 
					//System.out.println("RULE5");
					b.add(x); 
				}
			}
			//continue if we found only one cell
			if (b.size() == 1) { continue; }
			
			//number of cells that match have the same number of possible values
			if (c.getPossible().cardinality() == b.size()) {
				
				//remove this value from all other cells in this group
				for (cell z : g) {
					if ( !(z.isSolved()) && !(b.contains(z)) && (z.getPossible().intersects(c.getPossible())) ) {
						z.removePossible(c.getPossible());
						System.out.println("Rule 05:  Naked set found in " + g.toString() + ") for cells(" + b.toStringCells() + ") containing possibilities(" + c.getPossible() + ").  Removing these possibilities from cell(" + z.toStringCoords() + ").");
					}
				}
				
				
				
			}
			
			

		}
	}
}


